<?php  

App::import('Behavior', 'scope_out');

class PostTest extends CakeTestModel { 
    var $name = 'Post'; 
	
	var $actsAs = array(
		'Bindable' => array(),
		'ScopeOut'  => array(
				'Published' => array('conditions' => array('Post.published' => 1)),
				'PublishedFld' => array('conditions' => array('Post.published' => 1), 'fields'=>array('id','subject')),
				'UnpublishedAsc' => array('conditions' => array('Post.published' => 0)),
				'UnpublishedDesc' => array('conditions' => array('Post.published' => 0), 'order' => 'id desc'),
				'UnpublishedDesc2' => array('conditions' => array('Post.published' => 0), 'order' => array('id'=>'desc')),
				'UnpublishedDescLimited' => array('conditions' => array('Post.published' => 0), 'order' => 'id desc', 'limit'=>1),
				'TaggedTagD' => array('conditions' => array('Post.tag' => 'like %TagD%')),
		),
	);
	
}

class ScopeOutTestCase extends CakeTestCase { 
	var $fixtures = array('Post'); 

	function endTest() {
		ClassRegistry::flush();
	}
	
	function testParameters() {
        $this->Post =& new PostTest(); 
		$ScopeOut =& new ScopeOutBehavior();

		$config=array('conditions' => array('Post.published'=>1));
		$result = $ScopeOut->__addMaping($this->Post, 'Published', $config);
		$expectedMap = array('/findPublished/'=>'findScoped', '/findAllPublished/'=>'findAllScoped');
		$expectedConf = array('mapMethods'=> array('findPublished'=>$config,'findAllPublished'=>$config));
		$this->assertEqual($ScopeOut->mapMethods, $expectedMap);
		$this->assertEqual($ScopeOut->settings['Post'], $expectedConf);

		
	}
	

    function testFindPublished() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findPublished(null,array('id'));
        $required=array('Post'=>array('id'=>2)); 
		$this->assertEqual($required,$get1);
	}

    function testFindPublishedNew() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findPublished('all',array('fields' => array('id'), 'conditions' => null));
		$required = 
			array (
				array ( 'Post' => array ('id' => 2)),
				array ( 'Post' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindPublishedFld() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findPublishedFld('all',array('fields' => array('id')));
		$required = 
			array (
				array ( 'Post' => array ('id' => 2)),
				array ( 'Post' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get1);
		
		$get2=$this->Post->findPublishedFld('all',array());
		$required = 
			array (
				array ( 'Post' => array ('id' => 2,'subject'=>'Post2')),
				array ( 'Post' => array ('id' => 3,'subject'=>'Post3')),
			)
		; 
		$this->assertEqual($required,$get2);
	}

    function testFindAllPublished() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findAllPublished(null,array('id'));
		$required = 
			array (
				array ( 'Post' => array ('id' => 2)),
				array ( 'Post' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindUnpublishedAscNew() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findUnpublishedAsc('all',array('fields' => array('id'), 'conditions' => null));
		$required = 
			array (
				array ( 'Post' => array ('id' => 1)),
				array ( 'Post' => array ('id' => 4)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindUnpublishedDescNew() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findUnpublishedDesc('all',array('fields' => array('id'), 'conditions' => null));
		$required = 
			array (
				array ( 'Post' => array ('id' => 4)),
				array ( 'Post' => array ('id' => 1)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindUnpublishedDesc2New() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findUnpublishedDesc2('all',array('fields' => array('id'), 'conditions' => null));
		$required = 
			array (
				array ( 'Post' => array ('id' => 4)),
				array ( 'Post' => array ('id' => 1)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindUnpublishedDescLim() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findUnpublishedDescLimited('all',array('fields' => array('id')));
		$required = 
			array (
				array ( 'Post' => array ('id' => 4)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindAllUnpublished() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findAllUnpublishedAsc(null,array('id'));
		$required = 
			array (
				array ( 'Post' => array ('id' => 1)),
				array ( 'Post' => array ('id' => 4)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindAllTagged() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findAllTaggedTagD(null,array('id'));
		$required = 
			array (
				array ( 'Post' => array ('id' => 3)),
				array ( 'Post' => array ('id' => 4)),
			)
		; 
		$this->assertEqual($required,$get1);
	}

    function testFindAllPublishedBy() { 
        $this->Post =& new PostTest(); 
		$get1=$this->Post->findAllPublishedById(3,array('id'));
		$required = 
			array (
				array ( 'Post' => array ('id' => 3))
			)
		; 
		$this->assertEqual($required,$get1);
	}

	
    function testFindBySubject() { 
        $this->Post =& new PostTest(); 

		$get1=$this->Post->findBySubject('Post1',array('id','subject'));
		
		$required = array('Post' => array('id' => 1,'subject' => 'Post1')); 
		
		$this->assertEqual($get1, $required); 
		$get2=$this->Post->findAllBySubject('Post1',array('id','subject'));
		$required = 
			array(
				0 =>array('Post'=>array('id' => 1,'subject' => 'Post1'))
			); 
		$this->assertEqual($get2, $required); 
    } 
     
  

	

} 
?>